#include "QtWidgetsClass.h"

#include <iostream>
#include <mutex>
#include <qobject.h>
#include "rindo_view3d_widget.h"
//#include "moc_rindo_view3d_widget.cpp"
#include "rindo_view3d_widget.h"

extern void ReadCloud(const char* filename, cv::Mat& xyz);

struct c
{
	cv::Mat xyz;
	cv::Mat depth;
	cv::Mat gray;
	int id;
};
std::vector<c> vecc;

QtWidgetsClass::QtWidgetsClass(QWidget* parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	mpViewWidget = new rindo_view3d_widget;
	ui.verticalLayout->addWidget(mpViewWidget);

	mpViewWidget->SetPointCloudNanValue(0);
	mpViewWidget->SetPointCloudNanValueVisible(false);
	mpViewWidget->RegisterGetLog(std::bind(&QtWidgetsClass::AddLog, this, std::placeholders::_1, std::placeholders::_2));

	mpViewWidget->SetPointCloudSample(1);
	mpViewWidget->SetDisplayMode(DisplayType::HEIGHT_MAP_CLOUD);
	mpViewWidget->SetViewHeaderVisible(false);

	connect(ui.pushButton, &QPushButton::clicked, this, [=] {
		//if (!vecc.size())return;
		//auto& [points, depth, gray, id] = vecc.back();
		//std::cout << "show:"<< id<< mpViewWidget->AddPointCloud(points, depth, gray,id) << std::endl;
		//vecc.pop_back();

		mpViewWidget->AddPLYData("D:\\ampackData\\222.ply",999);
		//cv::Mat xyz;
		//ReadCloud("D:\\ampackData\\A-Robot\\GaGaGa20240428016.txt", xyz);
		//ReadCloud("D:\\ampackData\\A-Robot\\SendXYZABC\\GaGaGa20240428016\\L020053H_YA1.txt", xyz);
		//mpViewWidget->AddPointCloud(xyz, 2);
		});
}

QtWidgetsClass::~QtWidgetsClass()
{
	delete mpViewWidget;
	mpViewWidget = nullptr;
}

void QtWidgetsClass::AddPLYData(const char* file_name, int id)
{
	std::cout << "show:" << mpViewWidget->AddPLYData(file_name, id) << std::endl;
}

void QtWidgetsClass::AddPointCloud(cv::Mat points, int id)
{
	std::cout << "show:" << mpViewWidget->AddPointCloud(points, id) << std::endl;
}
void QtWidgetsClass::AddPointCloud(cv::Mat points, cv::Mat depth, cv::Mat gray, int id)
{
	//c cc;
	//cc.depth = depth;
	//cc.gray = gray;
	//cc.id = id;
	//cc.xyz = points;
	//vecc.push_back(cc);
	std::cout << "show:" << mpViewWidget->AddPointCloud(points, depth, gray, id) << std::endl;
}

void QtWidgetsClass::AddLog(std::string log, bool is_info)
{
	std::cout << is_info ? "info:" : "error:";
	std::cout << log << std::endl;
}
